{% extends 'base.html' %}
{% load static %}

{% block title %}匹配状态 - Creeps对战平台{% endblock %}

{% block extra_css %}
<style>
    body {
        background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
        min-height: 100vh;
        color: rgba(255, 255, 255, 0.9);
    }

    .match-card {
        background: linear-gradient(135deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.05));
        backdrop-filter: blur(10px);
        border: 1px solid rgba(100, 150, 255, 0.3);
        border-radius: 20px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
        transition: all 0.3s ease;
    }

    .match-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 15px 40px rgba(100, 150, 255, 0.2);
        border-color: rgba(100, 150, 255, 0.5);
    }

    .match-header {
        background: linear-gradient(135deg, rgba(100, 150, 255, 0.15), rgba(0, 212, 255, 0.1));
        border-bottom: 1px solid rgba(100, 150, 255, 0.3);
        color: rgba(255, 255, 255, 0.95);
        border-radius: 20px 20px 0 0;
        padding: 20px 25px;
    }

    .match-header h4 {
        color: rgba(255, 255, 255, 0.95);
        font-weight: 600;
        margin: 0;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .match-header i {
        color: #6496ff;
    }

    .info-card {
        background: rgba(255, 255, 255, 0.08);
        border: 1px solid rgba(100, 150, 255, 0.2);
        border-radius: 15px;
        transition: all 0.3s ease;
        margin-bottom: 15px;
    }

    .info-card:hover {
        background: rgba(255, 255, 255, 0.12);
        border-color: rgba(100, 150, 255, 0.4);
        transform: translateY(-2px);
    }

    .info-card .card-body {
        padding: 20px;
    }

    .info-card h6 {
        color: rgba(255, 255, 255, 0.8);
        font-weight: 500;
        margin-bottom: 10px;
    }

    .info-card p {
        color: rgba(255, 255, 255, 0.95);
        font-weight: 600;
        margin: 0;
        font-size: 1.1rem;
    }

    .btn-gradient {
        background: linear-gradient(135deg, #6496ff, #00d4ff);
        border: none;
        color: white;
        padding: 12px 25px;
        border-radius: 25px;
        font-weight: 600;
        transition: all 0.3s ease;
        box-shadow: 0 4px 15px rgba(100, 150, 255, 0.3);
    }

    .btn-gradient:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(100, 150, 255, 0.4);
        color: white;
    }

    .btn-outline-gradient {
        background: transparent;
        border: 2px solid rgba(100, 150, 255, 0.6);
        color: rgba(255, 255, 255, 0.9);
        padding: 10px 20px;
        border-radius: 20px;
        font-weight: 500;
        transition: all 0.3s ease;
        backdrop-filter: blur(5px);
    }

    .btn-outline-gradient:hover {
        background: linear-gradient(135deg, rgba(100, 150, 255, 0.2), rgba(0, 212, 255, 0.2));
        border-color: rgba(100, 150, 255, 0.8);
        color: white;
        transform: translateY(-2px);
        box-shadow: 0 4px 15px rgba(100, 150, 255, 0.3);
    }

    .btn-danger-gradient {
        background: linear-gradient(135deg, #dc3545, #c82333);
        border: none;
        color: white;
        padding: 10px 20px;
        border-radius: 20px;
        font-weight: 500;
        transition: all 0.3s ease;
        box-shadow: 0 4px 15px rgba(220, 53, 69, 0.3);
    }

    .btn-danger-gradient:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(220, 53, 69, 0.4);
        color: white;
    }

    .spinner-border {
        width: 3rem;
        height: 3rem;
        border-width: 0.3em;
        color: #6496ff;
    }

    .no-match-container {
        text-align: center;
        padding: 60px 20px;
    }

    .no-match-container i {
        color: rgba(255, 255, 255, 0.4);
        margin-bottom: 20px;
    }

    .no-match-container h5 {
        color: rgba(255, 255, 255, 0.8);
        margin-bottom: 15px;
    }

    .no-match-container p {
        color: rgba(255, 255, 255, 0.6);
        margin-bottom: 30px;
    }
</style>
{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="match-card">
                <div class="match-header">
                    <h4>
                        <i class="fas fa-clock"></i> 匹配状态
                    </h4>
                </div>
                <div class="card-body">
                    {% if current_match %}
                    <div class="text-center">
                        <div class="mb-4">
                            <div class="spinner-border text-primary" role="status">
                                <span class="visually-hidden">匹配中...</span>
                            </div>
                        </div>

                        <h5 class="mb-3">正在寻找对手...</h5>

                        <div class="row mb-4">
                            <div class="col-md-6">
                                <div class="info-card">
                                    <div class="card-body">
                                        <h6 class="card-title">匹配模式</h6>
                                        <p class="card-text">{{ current_match.get_match_mode_display }}</p>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="info-card">
                                    <div class="card-body">
                                        <h6 class="card-title">等待时间</h6>
                                        <p class="card-text" id="waiting-time">计算中...</p>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row mb-4">
                            <div class="col-12">
                                <div class="info-card">
                                    <div class="card-body">
                                        <h6 class="card-title">使用代码</h6>
                                        <p class="card-text">{{ current_match.code.title }}</p>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="d-flex gap-3 justify-content-center">
                            <button class="btn-outline-gradient" onclick="checkMatchStatus()">
                                <i class="fas fa-sync-alt"></i> 刷新状态
                            </button>
                            <form method="post" action="{% url 'battles:cancel_match' %}" class="d-inline">
                                {% csrf_token %}
                                <button type="submit" class="btn-danger-gradient" onclick="return confirm('确定要取消匹配吗？')">
                                    <i class="fas fa-times"></i> 取消匹配
                                </button>
                            </form>
                        </div>
                    </div>
                    {% else %}
                    <div class="no-match-container">
                        <i class="fas fa-info-circle fa-3x"></i>
                        <h5>当前没有进行中的匹配</h5>
                        {% if not user.is_staff %}
                            {% if battle_creation_enabled %}
                                <p>您可以返回创建新的对战匹配</p>
                                <a href="{% url 'battles:battle_create' %}" class="btn-gradient">
                                    <i class="fas fa-plus"></i> 创建对战
                                </a>
                            {% else %}
                                <p>创建对战功能暂时不可用</p>
                                <button class="btn-gradient disabled" onclick="showFeatureDisabledAlert()" title="点击查看详情">
                                    <i class="fas fa-plus"></i> 创建对战
                                </button>
                            {% endif %}
                        {% else %}
                            <p>管理员账号不能创建对战匹配</p>
                            <a href="{% url 'battles:battle_list' %}" class="btn-gradient">
                                <i class="fas fa-list"></i> 返回对战列表
                            </a>
                        {% endif %}
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // 自动刷新匹配状态
    let statusCheckInterval;

    function checkMatchStatus() {
        fetch('/battles/api/match/status/')
            .then(response => response.json())
            .then(data => {
                if (data.status === 'matched') {
                    // 匹配成功，跳转到对战页面
                    window.location.href = data.redirect_url;
                } else if (data.status === 'expired') {
                    // 匹配过期
                    {% if not user.is_staff %}
                        {% if battle_creation_enabled %}
                            alert('匹配已过期，请重新创建匹配');
                            window.location.href = '{% url "battles:battle_create" %}';
                        {% else %}
                            alert('匹配已过期，但创建对战功能已被管理员暂时关闭。\n\n如有疑问，请联系系统管理员。');
                            window.location.href = '{% url "battles:battle_list" %}';
                        {% endif %}
                    {% else %}
                        alert('匹配已过期，请重新创建匹配');
                        window.location.href = '{% url "battles:battle_list" %}';
                    {% endif %}
                } else if (data.status === 'no_match') {
                    // 没有匹配
                    {% if not user.is_staff %}
                        {% if battle_creation_enabled %}
                            window.location.href = '{% url "battles:battle_create" %}';
                        {% else %}
                            alert('创建对战功能已被管理员暂时关闭。\n\n如有疑问，请联系系统管理员。');
                            window.location.href = '{% url "battles:battle_list" %}';
                        {% endif %}
                    {% else %}
                        window.location.href = '{% url "battles:battle_list" %}';
                    {% endif %}
                }
                // 如果是waiting状态，继续等待
            })
            .catch(error => {
                console.error('检查匹配状态失败:', error);
            });
    }

    // 更新等待时间
    function updateWaitingTime() {
        const waitingTimeElement = document.getElementById('waiting-time');
        if (waitingTimeElement && '{{ current_match.created_at|date:"c" }}') {
            const now = new Date();
            const startTime = new Date('{{ current_match.created_at|date:"c" }}');
            const diff = Math.floor((now - startTime) / 1000);
            const minutes = Math.floor(diff / 60);
            const seconds = diff % 60;
            waitingTimeElement.textContent = `${minutes}分${seconds}秒`;
        }
    }

    // 页面加载完成后开始定时检查
    document.addEventListener('DOMContentLoaded', function () {
        {% if current_match %}
        // 每5秒检查一次匹配状态
        statusCheckInterval = setInterval(checkMatchStatus, 5000);

        // 每秒更新等待时间
        updateWaitingTime();
        setInterval(updateWaitingTime, 1000);
        {% endif %}
    });

    // 页面卸载时清除定时器
    window.addEventListener('beforeunload', function () {
        if (statusCheckInterval) {
            clearInterval(statusCheckInterval);
        }
    });

    // 显示功能禁用提示
    function showFeatureDisabledAlert() {
        alert('创建对战功能已被管理员暂时关闭。\n\n如有疑问，请联系系统管理员。');
    }
</script>
{% endblock %}